<?php

	namespace App\Service\Approval\ApprovalCreditProject;

	use App\Service\Approval\DingTalkApprovalInterFace;
    use App\Models\modelCreditProjectManage;

	class ApprovalCreditProjectCreditAmountAjustReviw implements DingTalkApprovalInterFace
	{
		public function __construct()
		{
			$this->m_cHelper					= app()->gcHelper					;
			$this->m_modelCreditProjectManage	= new modelCreditProjectManage()	;

		}
		
		/* 映射信用项目额度的调增调减表id => 信用项目表id
		 *
		 */ 
		private function mapCreditProjectAdjustQuotaIdToCreditProjectId($n_adjust_quota_id)
		{
			return $this->m_cHelper->toObjectEx	(
													[
														# credit project id
														#
														'credit_project_id'					=> $this->m_cHelper->gfDbQuery()->getDbValue("select credit_project_id from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_CREDIT_PROJECT_EX_ADJUST_QUOTA_TABLE_NAME')." where id=?",[$n_adjust_quota_id])
														,

														# credit project adjust quota id
														#
														'credit_project_adjust_quota_id'	=> $n_adjust_quota_id
													]
												);
		}

		/* 审批开始	 
		 *
		 */
		public function approvalStart($processInstanceId,$sourceId/* 审批额外携带的id => 信用项目额度的调增调减表id */,$processStatus)
		{ 
			# 1. 修改主表 cy_credit_project_ex.review_adjust_quota_step_status	= CREDIT_PROJECT_STATUS_7_AMOUNT_ADJUST_REVIEW
			#
			$this->m_cHelper->gfDbQuery()->updateByArrayWithWhere	(
																		$this->m_cHelper->gfGetTableNameByConstKey('CY_CREDIT_PROJECT_EX_TABLE_NAME')
																		,
																		["where id=?",[$this->mapCreditProjectAdjustQuotaIdToCreditProjectId($sourceId)->credit_project_id]]
																		,
																		[
																			'credit_project_status'	=> modelCreditProjectManage::CREDIT_PROJECT_STATUS_7_AMOUNT_ADJUST_REVIEW 
																		]
																	);

			# 2. 修改次表 cy_credit_project_ex_adjust_quota.approve_status		= 1 	
			#
			$this->m_cHelper->gfDbQuery()->updateByArrayWithWhere	(
																		$this->m_cHelper->gfGetTableNameByConstKey('CY_CREDIT_PROJECT_EX_ADJUST_QUOTA_TABLE_NAME')
																		,
																		["where id=?",[$sourceId]]
																		,
																		[
																			'approve_status'	=> modelCreditProjectManage::CREDIT_PROJECT_ADJUST_QUOTA_REVIEW_1_WAIT_FOR_REVIEW
																		]
																	);

		}
		
		/* 审批完成  
		 *
		 */
		public function approvalAgreeEnd($processInstanceId, $sourceId,$processStatus)
		{
			# 1. 修改主表 cy_credit_project_ex.review_adjust_quota_step_status	= CREDIT_PROJECT_STATUS_2_REVIEW_OK
			#
			$this->m_cHelper->gfDbQuery()->updateByArrayWithWhere	(
																		$this->m_cHelper->gfGetTableNameByConstKey('CY_CREDIT_PROJECT_EX_TABLE_NAME')
																		,
																		["where id=?",[$this->mapCreditProjectAdjustQuotaIdToCreditProjectId($sourceId)->credit_project_id]]
																		,
																		[
																			'credit_project_status'	=> modelCreditProjectManage::CREDIT_PROJECT_STATUS_2_REVIEW_OK 
																		]
																	);

			#
			#
			$approve_status					= 0;	

			# 丁丁审批后阿里服务器远程回调到此处
			#
			#
			# tips:
			#		1. 由于审核期间属于项目不确定期间,客户的信用额度可能会发生变化
			#		2. 所有这里要再次检查下游客户是否满足调增调减的额度限制要求
			#
			# 
			#
			$credit_project_adjust_quota	= $this->m_cHelper->toObjectEx($this->m_cHelper->gfDbQuery()->getDbOneRow("select * from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_CREDIT_PROJECT_EX_ADJUST_QUOTA_TABLE_NAME')." where id=?",[$sourceId]));	# 通过id获取调增调减的申请数据
			if (
					$this->m_modelCreditProjectManage->verifyCreditProjectAdjustQuotaRange	(
																								$this->mapCreditProjectAdjustQuotaIdToCreditProjectId($sourceId)->credit_project_id																				# credit project id
																								,		
																								$credit_project_adjust_quota->credit_project_adjust_type																										# [ 1=调增 2=调减 ]
																								,
																								$credit_project_adjust_quota->credit_project_quota																												# 调整的额度大小[ 单位:元 ]
																								,
																								$error_detail
																								,
																								b_check_credit_project_adjust_quota_count:false																													# 跳过申请记录检查
																							)
			)
			{
				$approve_status				= modelCreditProjectManage::CREDIT_PROJECT_ADJUST_QUOTA_REVIEW_2_REVIEW_OK;																																			# 下游客户信用额度足够 => 回调验证通过
			}
			else
			{
				$approve_status				= modelCreditProjectManage::CREDIT_PROJECT_ADJUST_QUOTA_REVIEW_4_REVIEW_OK_BUT_CUSTOMER_NOT_ENOUGH_CREDIT_QUOTA;																									# 下游客户信用额度不足 => 回调验证失败
			}

			# 2. 修改次表 cy_credit_project_ex_adjust_quota.approve_status		= 2 	
			#
			$this->m_cHelper->gfDbQuery()->updateByArrayWithWhere	(
																		$this->m_cHelper->gfGetTableNameByConstKey('CY_CREDIT_PROJECT_EX_ADJUST_QUOTA_TABLE_NAME')
																		,
																		["where id=?",[$sourceId]]
																		,
																		[
																			'approve_status'	=> $approve_status
																		]
																	);
		}

		/* 审批失败 
		 *
		 */
		public function approvalRefuseEnd($processInstanceId,$sourceId,$processStatus)
		{
			# 1. 修改主表 cy_credit_project_ex.review_adjust_quota_step_status	= CREDIT_PROJECT_STATUS_2_REVIEW_OK
			#
			$this->m_cHelper->gfDbQuery()->updateByArrayWithWhere	(
																		$this->m_cHelper->gfGetTableNameByConstKey('CY_CREDIT_PROJECT_EX_TABLE_NAME')
																		,
																		["where id=?",[$this->mapCreditProjectAdjustQuotaIdToCreditProjectId($sourceId)->credit_project_id]]
																		,
																		[
																			'credit_project_status'	=> modelCreditProjectManage::CREDIT_PROJECT_STATUS_2_REVIEW_OK 
																		]
																	);

			# 2. 修改次表 cy_credit_project_ex_adjust_quota.approve_status		= 3 	
			#
			$this->m_cHelper->gfDbQuery()->updateByArrayWithWhere	(
																		$this->m_cHelper->gfGetTableNameByConstKey('CY_CREDIT_PROJECT_EX_ADJUST_QUOTA_TABLE_NAME')
																		,
																		["where id=?",[$sourceId]]
																		,
																		[
																			'approve_status'	=> modelCreditProjectManage::CREDIT_PROJECT_ADJUST_QUOTA_REVIEW_3_REVIEW_REJECT
																		]
																	);
 
		}
	} 

